import {Socket} from "net";
import log from "./logger";
import {iconv} from "../node-test";

// const printSock = new Socket();
// printSock.connect(9100, "192.168.10.57", () => {
//     log.log(`printSock connected to: 192.168.10.57:9100`);
// });
// printSock.on('error', function (error) {
//     log.error('printSock error', error);
// });
// printSock.on('close', () => {
//     log.error('close下线了');
// });

const cut = Buffer.from([0x1D, 0x56, 0x01]);
const cr = Buffer.from([0x0A]);
const cr5 = Buffer.from([0x1B, 0x64, 0x05]);
const font0 = Buffer.from([0x1D, 0x21, 0x00]);
const font1 = Buffer.from([0x1D, 0x21, 0x11]);
const alignleft = Buffer.from([0x1B, 0x61, 0x00]);
const aligncenter = Buffer.from([0x1B, 0x61, 0x01]);
const indentleft = Buffer.from([0x1D, 0x4C, 0x20, 0x00]);
const status = Buffer.from([0x1b, 0x76]);

function parser(printSock: any, str: string, index: number) {
    for (let line of str.split("\n")) {
        const cmd = line.split(":")[0];
        const param = line.replace(cmd + ':', '');
        if (cmd === 'font1') {
            printSock.write(font1);
        } else if (cmd === 'align-center') {
            printSock.write(aligncenter);
        } else if (cmd === 'align-left') {
            printSock.write(alignleft);
        } else if (cmd === 'text') {
            printSock.write(iconv.encode(param, 'gb18030'));
            printSock.write(cr);
        } else if (cmd === 'font0') {
            printSock.write(font0);
        } else if (cmd === 'indent-left-20') {
            printSock.write(indentleft);
        } else {
            log.error(`命令${cmd}不支持`)
            throw `命令${cmd}不支持`;
        }
    }
    printSock.write(font0);
    const no = ['一', '二', '三', '四', '五', '六', '七'][index];
    printSock.write(iconv.encode(`第${no}联`, 'gb18030'));
    printSock.write(cr);
    printSock.write(cr5);
    printSock.write(cut);
}

const str = "font1\n" +
    "align-center\n" +
    "text:${platformName}数字站台过磅单\n" +
    "text:\n" +
    "font0\n" +
    "align-left\n" +
    "indent-left-20\n" +
    "text:--------------------------------------------\n" +
    "text:车牌号：${carNum}\n" +
    "text:工单号：${code}\n" +
    "text:--------------------------------------------\n" +
    "text:矿发：${primaryTime}\n" +
    "text:发货企业：${senderName}\n" +
    "text:收货企业：${customerName}\n" +
    "text:起点：${startPointName}\n" +
    "text:终点：${platformName}（${spaceName}）\n" +
    "text:煤种：${productName}\n" +
    "text:集装箱号：${containerNum}\n" +
    "text:备注：${remark}\n" +
    "text:--------------------------------------------\n" +
    "text:站台毛重：${grossWeight}吨\n" +
    "text:站台皮重：${tareWeight}吨\n" +
    "text:扣减重量：${e_confirmDeductWeight}吨\n" +
    "text:站台净重：${netWeight}吨\n" +
    "text:\n" +
    "text:毛重时间：${firstTime}\n" +
    "text:皮重时间：${secondTime}\n" +
    "text:打印时间：${now}\n" +
    "text:--------------------------------------------\n" +
    "text:矿发净重：${primaryWeight}吨\n" +
    "text:亏吨：${lossWeight}吨\n" +
    "text:--------------------------------------------\n" +
    "font1\n" +
    "align-center\n" +
    "text:\n" +
    "text:互链时空  数字站台\n" +
    "text:"

function print() {
    const printSock = new Socket();
    printSock.connect(9100, "192.168.30.70", () => {
        log.log(`printSock connected to: 192.168.30.70:9100, print...`);
        // parser(printSock, str, 1);
        printSock.write(Buffer.from([0x10, 0x04, 0x04]));
        setTimeout(() => {
            printSock.destroy()
        }, 2000)
    });
    printSock.on('data', (data: Buffer) => {
        log.info("printSock data:", data.toString('hex'));
    })
    printSock.on('error', function (error) {
        log.error('printSock error', error);
    });
    printSock.on('close', () => {
        log.error('close下线了');
    });
}

setTimeout(() => {
    print()
}, 5000)
setInterval(() => {
    print()
}, 1000 * 60)
